<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文生图</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 20px;
    }
    #image-container {
      margin-top: 20px;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    img {
      max-width: 100%;
      height: auto;
    }
    .loader {
      border: 16px solid #f3f3f3;
      border-radius: 50%;
      border-top: 16px solid #3498db;
      width: 120px;
      height: 120px;
      animation: spin 2s linear infinite;
    }
    @keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }
  </style>
</head>
<body>
<h1>生成图像</h1>
<button id="generate-button">生成图像</button>
<div id="image-container">
  <div id="loader" class="loader" style="display: none;"></div>
  <img id="generated-image" src="" alt="Generated Image" style="display: none;">
</div>
<script>
  document.getElementById('generate-button').addEventListener('click', function() {
    document.getElementById('loader').style.display = 'block';
    document.getElementById('generated-image').style.display = 'none';
    fetch('/ai/image', { method: 'POST' })
            .then(response => response.json())
            .then(data => {
              document.getElementById('loader').style.display = 'none';
              if (data && data.data && data.data[0] && data.data[0].b64Image) {
                const imgSrc = 'data:image/png;base64,' + data.data[0].b64Image;
                document.getElementById('generated-image').src = imgSrc;
                document.getElementById('generated-image').style.display = 'block';
              } else {
                alert('生成图像失败');
              }
            })
            .catch(error => {
              document.getElementById('loader').style.display = 'none';
              alert('请求失败：' + error);
            });
  });
</script>
</body>
</html>
